<template>
  <div class="xtx-dialog" :class="{ fade: fade }" v-show="isShow">
    <div class="wrapper" :class="{ fade: fade }">
      <div class="header">
        <h3>{{ props.title }}</h3>
        <a href="JavaScript:;" @click="close" class="iconfont icon-close-new"></a>
      </div>
      <div class="body">
        <!-- 默认插槽来接收主体内容-->
        <slot />
      </div>
      <div class="footer">
        <!-- 具名插槽footer来接收操作按钮 -->
        <slot name="footer">
         <!--  <xtx-button @click="close" type="gray" style="margin-right:20px">取消</xtx-button>
          <xtx-button @click="close" type="primary">确认</xtx-button> -->
        </slot>
      </div>
    </div>
  </div>
</template>
<script setup>
import { useVModel } from '@vueuse/core'
import { ref, onMounted, defineProps, defineEmits } from 'vue'
const props = defineProps({
  // 标题由使用该对话框的组件传递
  title: {
    type: String,
    default: ''
  },
  visible: {
    type: Boolean,
    default: false
  }
})
// 父组件的自定义事件
const emit = defineEmits(['update:visible'])
const isShow = useVModel(props, 'visible', emit)
// 改变显示和隐藏
function close() {
  isShow.value = false
  // 向外提供自定义事件
  emit('closed')
}
// 动画效果
const fade = ref(false)
onMounted(() => {
  // 结构和样式同时加上无过度效果，需要些延时。
  setTimeout(() => {
    fade.value = true
  }, 0)
})
</script>
<style scoped lang="less">
.xtx-dialog {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8887;
  background: rgba(0, 0, 0, 0);

  &.fade {
    transition: all 0.4s;
    background: rgba(0, 0, 0, .5);
  }

  .wrapper {
    width: 600px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    opacity: 0;

    &.fade {
      transition: all 0.4s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }

    .body {
      padding: 20px 40px;
      font-size: 16px;

      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }

    .footer {
      text-align: center;
      padding: 10px 0 30px 0;
    }

    .header {
      position: relative;
      height: 70px;
      line-height: 70px;
      padding: 0 20px;
      border-bottom: 1px solid #f5f5f5;

      h3 {
        font-weight: normal;
        font-size: 18px;
      }

      a {
        position: absolute;
        right: 25px;
        top: 25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;

        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>